package com.google.code.bmi;

import java.text.DecimalFormat;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class BmiActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getViews();
        restorePrefs();
        setListeners();
    }
    @Override
    protected void onPause() {
    	super.onPause();
    	SharedPreferences setting = getSharedPreferences(PREF, 0);
    	setting.edit().putString(PREF_HEIGHT, editHeight.getText().toString()).commit();
    }
    // private static final String TAG = "BMI";
    public static final String PREF = "BMI_PREF";
    public static final String PREF_HEIGHT = "BMI_HEIGHT";
    private void restorePrefs() {
    	SharedPreferences setting = getSharedPreferences(PREF, 0);
    	String height = setting.getString(PREF_HEIGHT, "");
    	if (! "".equals(height)) {
			editHeight.setText(height);
			editWeight.requestFocus();
		}
	}
    private void setListeners() {
    	submit_bmi.setOnClickListener(calcBMI);
    }
    private Button submit_bmi;
    private EditText editHeight;
    private EditText editWeight;
    private TextView viewResult;
    private TextView viewSuggest;
    private void getViews() {
    	submit_bmi = (Button) findViewById(R.id.submit_bmi);
		editHeight = (EditText) findViewById(R.id.height);
		editWeight = (EditText) findViewById(R.id.weight);
		viewResult = (TextView) findViewById(R.id.result_bmi);
		viewSuggest = (TextView) findViewById(R.id.suggest_bmi);
    }
    private void openAboutMe() {
    	AlertDialog.Builder viewAboutMe = new AlertDialog.Builder(BmiActivity.this);
    	viewAboutMe.setTitle(R.string.about_title);
    	viewAboutMe.setMessage(R.string.about_message);
    	viewAboutMe.setPositiveButton(R.string.submit_about, new DialogInterface.OnClickListener() {
    		
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO: dialog on click
			}
		});
    	viewAboutMe.setNegativeButton(R.string.browse_about_me, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Uri uri = Uri.parse(getString(R.string.about_me_uri));
				Intent toBrowseAboutMeOnline = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(toBrowseAboutMeOnline);
			}
		});
    	viewAboutMe.show();
    }
    private void openErrorMsg() {
    	Toast viewErrorMsg = Toast.makeText(BmiActivity.this, R.string.error_input, Toast.LENGTH_SHORT);
    	viewErrorMsg.show();
    }
    protected static final int MENU_ABOUT = Menu.FIRST;
    protected static final int MENU_QUIT = Menu.FIRST + 1;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	boolean rtn = super.onCreateOptionsMenu(menu);
    	menu.add(0, MENU_ABOUT, 0, R.string.menu_about);
    	menu.add(0, MENU_QUIT,  0, R.string.menu_quit);
    	return rtn;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	boolean rtn =  super.onOptionsItemSelected(item);
    	switch (item.getItemId()) {
		case MENU_ABOUT:
			openAboutMe();
			break;
		case MENU_QUIT:
			finish();
			break;
		default:
			break;
		}
    	return rtn;
    }
    private OnClickListener calcBMI = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			DecimalFormat nf = new DecimalFormat("0.00");
			Double height = Double.parseDouble(editHeight.getText().toString())/100;
			Double weight = Double.parseDouble(editWeight.getText().toString());
			Log.d("MAIN", height.toString());
			Log.d("MAIN", weight.toString());
			Double bmi = weight / (height * height);
			Log.d("MAIN", bmi.toString());
			viewResult.setText(getString(R.string.result_prefex) + nf.format(bmi));
			if (bmi > 25) {
				viewSuggest.setText(R.string.suggest_heavy);
			} else if (bmi < 20) {
				viewSuggest.setText(R.string.suggest_light);
			} else {
				viewSuggest.setText(R.string.suggest_normal);
			}
			Intent intent = new Intent();
			intent.setClass(BmiActivity.this, ReportActivity.class);
			Bundle bundle = new Bundle();
			bundle.putString("KEY_HEIGHT", editHeight.getText().toString());
			bundle.putString("KEY_WEIGHT", editWeight.getText().toString());
			intent.putExtras(bundle);
			startActivity(intent);
			// openAboutMe();
			// openErrorMsg();
		}
	};
}